package cn.android.support.v7.lib.sin.crown.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/9/29.
 */

public class ActionBar extends View implements View.OnTouchListener {
    public ActionBar(Context context) {
        super(context);
        init();
    }

    public ActionBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    protected int width = (int) ProportionUtils.realWidthPixels;
    protected int height = ProportionUtils.getInstance().adapterInt(125);
    protected int TextSize = ProportionUtils.getInstance().adapterInt(38);
    protected int TextColor = Color.parseColor("#444444");

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    protected Paint paint;

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setTextSize(TextSize);
        paint.setColor(TextColor);
        paint.setTextAlign(Paint.Align.CENTER);
        setLayerType(View.LAYER_TYPE_HARDWARE,paint);
        setOnTouchListener(this);//添加触摸事件
    }

    protected Bitmap left;//左边图标
    protected String title;//标题

    public void setLeftBimapAndTitle(Bitmap left, String title) {
        if (left != null) {
            this.left = left;
        }
        if (title != null) {
            this.title = title;
        }
        invalidate();
    }

    Region region;

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        if (left == null || title == null) {
            return;
        }
        paint.setColor(TextColor);
        canvas.drawColor(Color.parseColor("#FFFFFF"));
        int statusHeight = ProportionUtils.getInstance().getStatusHeight(getContext());
        int x = width / 27;
        int y = statusHeight + (height - statusHeight - left.getHeight()) / 2;
        canvas.drawBitmap(left, x, y, paint);
        canvas.drawText(title, width / 2, y + left.getHeight(), paint);
        paint.setColor(Color.parseColor("#bfbfbf"));
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        int line = ProportionUtils.getInstance().adapterInt(1);
        paint.setStrokeWidth(line);
        canvas.drawLine(0, height - line, width, height - line, paint);

        region = new Region();
        Path path = new Path();
        int add = 30;//额外范围增加
        path.addRect(x - add, y - add, x + left.getWidth() + add, y + left.getHeight() + add, Path.Direction.CCW);
        RectF rect = new RectF();
        path.computeBounds(rect, true);
        region.setPath(path, new Region((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom));
    }

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            //单击
            if (region.contains((int) e.getX(), (int) e.getY()) && baseListennerLeft != null) {
                baseListennerLeft.onResult(null);
            }
            return super.onSingleTapConfirmed(e);
        }
    };

    GestureDetector gestureDetector = new GestureDetector(simpleOnGestureListener);

    BaseListenner baseListennerLeft;

    //左边图标的点击回调事件
    public void setBaseListennerLeft(BaseListenner baseListennerLeft) {
        this.baseListennerLeft = baseListennerLeft;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        gestureDetector.onTouchEvent(motionEvent);
        return true;
    }
}
